package com.XiaoLongGroup.webtech.service.impl;/**
 * @author : SLCcccc
 * @date : 2023/5/12 10:49
 */

import com.XiaoLongGroup.webtech.entity.Article;
import com.XiaoLongGroup.webtech.entity.User;
import com.XiaoLongGroup.webtech.mapper.ArticleMapper;
import com.XiaoLongGroup.webtech.service.ArticleService;
import com.XiaoLongGroup.webtech.utils.Page;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 *@ClassName ArticleServiceImpl
 *@Description TODO
 *@Author SLCcccc
 *@Date 2023/5/12 10:49
 *@Version 1.0
 */
@Service
public class ArticleServiceImpl implements ArticleService {

    @Resource
    private ArticleMapper articleMapper;

    @Override
    public List<Article> getArticleList() {
        return articleMapper.getAllArticleList();
    }

    @Override
    public Article getArticleById(int id) {
        return articleMapper.getArticleByArticleId(id);
    }

    @Override
    public boolean addArticle(Article article) {
        // add
        try {
            if(articleMapper.addArticle(article) != 0) {
                return true;
            }
        } catch (Exception e) {
            return false;
        }
        return false;
    }

    @Override
    public Page<Article> getArticleListByNameLikeAndAuthorId(String authorId, String name, int page, int size) {
        Page<Article> articlePage = new Page<>(page,size);
        System.out.println(page+"  "+size);
        if(name!=null){
            articlePage.setRows(articleMapper.getArticleListByNameLikeAndAuthorId(authorId, name,(page-1)*size,size));
            articlePage.setTotal(articleMapper.getArticleCountByAuthorIdAndNameLike(name,authorId));
        }else{
            articlePage.setRows(articleMapper.getArticlesByAuthorId(authorId,(page-1)*size,size));
            articlePage.setTotal(articleMapper.getArticleCountByAuthorId(authorId));
        }

        return articlePage;
    }

    @Override
    public Page<Article> getArticleList(int page, int size){
        Page<Article> articlePage = new Page<>(page,size);
        articlePage.setRows(articleMapper.getArticleList((page-1)*size,size));
        articlePage.setTotal(articleMapper.getArticleCount(""));
        return articlePage;
    }

    @Override
    public Page<Article> getArticleListByNameLike(String name, int page, int size) {
        Page<Article> articlePage = new Page<>(page,size);
        articlePage.setRows(articleMapper.getArticleListByNameLike(name,(page-1)*size,size));
        articlePage.setTotal(articleMapper.getArticleCount(name));
        return articlePage;
    }

    @Override
    public Boolean ArticleInfoChange(Article article) {
        try {
            if (articleMapper.updateArticle(article)!=0){
                return true;
            }
        } catch (Exception e) {
            return false;
        }
        return false;
    }

    @Override
    public boolean deleteArticleById(int articleId) {
        return articleMapper.deleteArticleByArticleId(articleId) != 0;
    }
}
